#!/bin/bash
#2020年3月4日10:41:48
#auto backup mysql db
#by author www.jfedu.net
########################
SQL_DB="jfedu001"
SQL_USR="backup"
SQL_PWD="bak123456"
SQL_CMD="/usr/bin/mysql"
SQL_DIR="/data/backup/`date +%F`"
if [ ! -d $SQL_DIR ];then
        mkdir -p $SQL_DIR
        echo "The $SQL_DIR create succeeded."
fi
${SQL_CMD} -e "grant select,lock tables on *.* to $SQL_USR@'localhost' identified by \"$SQL_PWD\";"
${SQL_CMD}dump -u$SQL_USR -p$SQL_PWD ${SQL_DB} > $SQL_DIR/${SQL_DB}.sql
if [ $? -eq 0 ];then
        echo -e "\033[32m--------------------\033[0m"
        echo -e "\033[32mThe ${SQL_DB} backup succeeded.\033[0m"
        echo "$SQL_DIR/"
        ls -l $SQL_DIR/
else
        echo -e "\033[32m--------------------\033[0m"
        echo -e "\033[32mThe ${SQL_DB} backup falied.\033[0m"
fi